import { reqAddRole, reqBatchRemove, reqRemoveRole, reqRoleInfo, reqUpdateRole } from "@/api/acl/role"

const actions = {
    // 发送获取角色信息的action
    async getRoleInfo({ commit }, data) {
        const { page, limit, roleName } = data
        const result = await reqRoleInfo(page, limit, roleName)
        if (result.code === 20000) {
            commit('GETROLEINFO', result.data)
        }
    },
    // 发送添加新角色的action
    async getAddRole(_, data) {
        const result = await reqAddRole(data)
        console.log(result);
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    },
    // 发送要批量删除角色的acton
    async batchRemove(_, idList) {
        const result = await reqBatchRemove(idList)
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    },
    // 发送修改角色的action
    async updateRoleInfo(_, role) {
        const result = await reqUpdateRole(role)
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    },
    // 发送删除单个角色的action
    async removeRole(_, id) {
        const result = await reqRemoveRole(id)
        if (result.code === 20000) {
            return 'ok'
        }
        return Promise.reject()
    }
}
const mutations = {
    GETROLEINFO(state, roleInfo) {
        state.roleInfo = roleInfo
    }
}
const state = {
    // 角色列表
    roleInfo: {}
}
export default {
    // 开启命名空间
    namespaced: true,
    actions,
    mutations,
    state
}